package cn.tedu.baking.controller;

import cn.tedu.baking.pojo.dto.ContentSaveDTO;
import cn.tedu.baking.pojo.vo.ContentDetailVO;
import cn.tedu.baking.pojo.vo.ContentIndexVO;
import cn.tedu.baking.pojo.vo.ContentVO;
import cn.tedu.baking.response.common.JsonResult;
import cn.tedu.baking.service.IContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author：毕龙
 * Date: 2024/8/15 14:30
 */
@RestController
@RequestMapping("/v1/contents/")
@Slf4j
public class ContentController {

    @Autowired
    private IContentService iContentService;

    public ContentController(){
        log.debug("创建ContentController对象");
    }
    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(ContentSaveDTO contentSaveDTO){
        log.debug("获取的数据：{}", contentSaveDTO);
        iContentService.saveOrUpdate(contentSaveDTO);
        return JsonResult.ok();
    }
    @GetMapping("/selectAll/{createBy}/{type}")
    public JsonResult getContentsByCreateByAndType(@PathVariable Integer createBy ,
                                            @PathVariable Integer type ){
        log.debug("获取的数据：{} ----- {}", createBy,type);
        List<ContentVO> vo = iContentService.getContentsByCreateBy(createBy,type);
        return JsonResult.ok(vo);
    }
    @DeleteMapping("/delete/{id}")
    public JsonResult deleteContentsById(@PathVariable Integer id){
        log.debug("获取的数据：{} ", id);
        iContentService.deleteContentsById(id);
        return JsonResult.ok();
    }

    @GetMapping("/getContent/{id}")
    public JsonResult getContentsById(@PathVariable Integer id){
        log.debug("获取的数据：{} ", id);
        ContentVO  vo = iContentService.getContentsById(id);
        return JsonResult.ok(vo);
    }
    @GetMapping("/{type}/{categoryId}/index")
    public JsonResult getContentsByTypeAndCategoryId(@PathVariable Integer type ,
                                                   @PathVariable Integer categoryId ){
        log.debug("获取的数据：{} ----- {}", type,categoryId);

        List<ContentIndexVO> vo =  iContentService.getContentsByTypeAndCategoryId(type,categoryId);
        return JsonResult.ok(vo);
    }

    @GetMapping("/{type}/list")
    public JsonResult getContentsByTypeAndCategoryId(@PathVariable Integer type ){
        log.debug("获取的数据：{} ", type);

        List<ContentIndexVO> vo =  iContentService.getContentsByTypeAndCategoryId(type);
        return JsonResult.ok(vo);
    }

    @GetMapping("/like")
    public JsonResult getContentslikeTitle(String title ){
        log.debug("获取的数据：{} ", title);

        List<ContentIndexVO> vo =  iContentService.getContentslikeTitle(title);
        return JsonResult.ok(vo);
    }

    @GetMapping("/{id}")
    public JsonResult getContentsByContentId(@PathVariable Integer id ){
        log.debug("获取的数据：{} ", id);

        ContentDetailVO vo =  iContentService.getContentsByContentId(id);
        return JsonResult.ok(vo);
    }

    @PutMapping("/{id}")
    public JsonResult updateViewCountById(@PathVariable Integer id ){
        log.debug("获取的数据：{} ", id);

        iContentService.updateViewCountById(id);
        return JsonResult.ok();
    }

    @GetMapping("/getContents")
    public JsonResult getContentsByCreateByAndContentId( Integer createBy ,
                                                         Integer id ){
        log.debug("获取的数据：{} ----- {}", createBy,id);

        List<ContentIndexVO> vo =  iContentService.getContentsByCreateByAndContentId(createBy,id);
        return JsonResult.ok(vo);
    }


}
